home *** CD-ROM | disk | FTP | other *** search
- /* Copyright (c) Silicon Graphics, Inc. 1996 */
-
- /* subtexture.c
- * A program to demonstrate the use of the EXT_subtexture
- * extention. It reads an image and texture maps it onto a polygon
- * in the left half of the window. It then redefines a subtexture
- * cut out of the middle of the image and maps the resulting texture
- * to the same polygon and displays it in the right half of the window.
- *
- * Escape key - exit the program
- */
- #include <GL/gl.h>
- #include <GL/glu.h>
- #include <GL/glut.h>
-
- #include <math.h>
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <malloc.h>
-
- #include "rgbImageFile.h" /* should be in ../../include */
-
- /* Function Prototypes */
-
- GLvoid initgfx( GLvoid );
- GLvoid drawScene( GLvoid );
- GLvoid reshape( GLsizei, GLsizei );
- GLvoid keyboard( GLubyte, GLint, GLint );
-
- GLvoid createSubImage( void );
- GLuint nearestPower( GLuint );
-
- void printHelp( char * );
-
- int getrenderer( char * );
- int getversion( char * );
-
- /* Global Definitions */
-
- /* The texel offsets and the dimensions of the subimages
- * must be multiples of 32.
- */
- #define SUBSIZE 128
-
- #define KEY_ESC 27 /* ascii value for the escape key */
-
- /* Global Variables */
-
- static GLubyte *image, *sImage, subImage[SUBSIZE*SUBSIZE*4];
- static GLsizei winWidth, winHeight;
- static GLsizei sWidth, sHeight;
-
- static GLint subx, suby;
- static GLint subwidth, subheight;
-
- GLvoid
- main( int argc, char *argv[] )
- {
- int imgWidth, imgHeight;
-
- glutInit( &argc, argv );
-
-
- if (argc < 2) {
- fprintf (stderr, "usage: %s <imageFileName>\n", argv[0]);
- exit (1);
- }
- image = (GLubyte *) rgbReadImageFile(argv[1], &imgWidth, &imgHeight);
-
- sWidth = nearestPower( imgWidth );
- sHeight = nearestPower( imgHeight );
- winWidth = sWidth * 2;
- winHeight = sHeight;
- if ( sWidth == imgWidth && sHeight == imgHeight ) {
- sImage = image;
- } else {
- sImage = (GLubyte *)malloc( sHeight*sWidth*4*sizeof(GLubyte) );
- gluScaleImage( GL_RGBA,
- imgWidth, imgHeight, GL_UNSIGNED_BYTE, image,
- sWidth, sHeight, GL_UNSIGNED_BYTE, sImage );
- }
-
- /* calculate the offsets and size for the subimage */
- subwidth = (sWidth <= SUBSIZE) ? sWidth/2 : SUBSIZE;
- subheight = (sHeight <= SUBSIZE) ? sHeight/2 : SUBSIZE;
- subx = sWidth/2 - subwidth/2;
- if (subx < 0) subx = 0;
- suby = sHeight/2 - subheight/2;
- if (suby < 0) suby = 0;
-
- glutInitWindowSize( 4, 4 );
- glutInitWindowSize( winWidth, winHeight );
- glutInitDisplayMode( GLUT_RGBA );
- glutCreateWindow( argv[0] );
-
- initgfx();
-
- glutKeyboardFunc( keyboard );
- glutReshapeFunc( reshape );
- glutDisplayFunc( drawScene );
-
- printHelp( argv[0] );
-
- glutMainLoop();
- }
-
-
- GLvoid
- printHelp( char *progname )
- {
- fprintf(stdout, "\n%s - uses subtexture extension to redefine "
- "a subimage in the middle\nof an images\n\n"
- "Escape key - exit the program\n",
- progname);
- }
-
- void
- initgfx( void )
- {
- if (!glutExtensionSupported("GL_EXT_subtexture") &&
- !(getrenderer("RE") && getversion("Irix 5.3"))) {
- fprintf( stderr,
- "GL_EXT_subtexture not supported on this machine\n");
- }
-
- glEnable(GL_TEXTURE_2D);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
-
- createSubImage();
- }
-
- /* Compute the nearest power of 2 number. */
- GLuint
- nearestPower( GLuint value )
- {
- int i = 1;
-
- if (value == 0) return -1; /* Error! */
- for (;;) {
- if (value == 1) return i;
- else if (value == 3) return i*4;
- value >>= 1; i *= 2;
- }
- }
-
- /* Pass the name of the desired renderer.
- * Returns 1 if this is the desired renderer, 0 otherwise.
- */
- int
- getrenderer(char *r)
- {
- const GLubyte *s;
-
- s = glGetString( GL_RENDERER );
- if ( !s || *s == '\0' ) return 0;
- return( strstr(s,r) == 0 ) ? 0 : 1;
- }
-
- /* Pass the name of the desired version.
- * Returns 1 if this is the desired version, 0 otherwise.
- */
- int
- getversion(char *v)
- {
- const GLubyte *s;
-
- s = glGetString( GL_VERSION );
- if ( !s || *s == '\0' ) return 0;
- return( strstr(s,v) == 0 ) ? 0 : 1;
- }
-
- GLvoid
- keyboard( GLubyte key, GLint x, GLint y )
- {
- switch (key) {
- case KEY_ESC: /* Exit whenever the Escape key is pressed */
- exit(0);
- }
- }
-
- GLvoid
- reshape( GLsizei width, GLsizei height )
- {
- /* make sure window size is odd */
- winWidth = width;
- winHeight = height;
-
- /* Create the first viewport - the left half of the window */
- glViewport( 0, 0, winWidth/2, winHeight );
-
- glMatrixMode( GL_PROJECTION );
- glLoadIdentity();
- gluOrtho2D( 0.0, (GLdouble) winWidth-1, 0.0, (GLdouble) winHeight-1 );
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity();
- glTranslatef( 0.375, 0.375, 0.0 );
- }
-
- void
- createSubImage(void)
- {
- int i, j;
- GLubyte *img = subImage;
-
- for (j = 0; j < subheight/4 * subwidth; j++) {
- *img++ = 0xff;
- *img++ = 0x00;
- *img++ = 0x00;
- *img++ = 0xff;
- }
- for (j = 0; j < subheight/4 * subwidth; j++) {
- *img++ = 0xff;
- *img++ = 0x00;
- *img++ = 0xff;
- *img++ = 0x00;
- }
- for (j = 0; j < subheight/4 * subwidth; j++) {
- *img++ = 0xff;
- *img++ = 0xff;
- *img++ = 0x00;
- *img++ = 0x00;
- }
- for (j = 0; j < subheight/4 * subwidth; j++) {
- *img++ = 0x00;
- *img++ = 0xff;
- *img++ = 0x00;
- *img++ = 0xff;
- }
- }
-
- GLvoid
- drawScene( GLvoid )
- {
- glClear( GL_COLOR_BUFFER_BIT );
-
- /* Create the first viewport - the left half of the window */
- glViewport( 0, 0, winWidth/2, winHeight );
-
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sWidth, sHeight, 0,
- GL_RGBA, GL_UNSIGNED_BYTE, sImage);
- glBegin(GL_QUADS);
- glTexCoord2f( 0, 0 ); glVertex2f( 0, 0 );
- glTexCoord2f( 1, 0 ); glVertex2f( winWidth, 0 );
- glTexCoord2f( 1, 1 ); glVertex2f( winWidth, winHeight );
- glTexCoord2f( 0, 1 ); glVertex2f( 0, winHeight );
- glEnd();
-
- /* Create the second viewport - right half of window */
- glViewport( winWidth/2 + 1, 0, winWidth/2, winHeight );
-
- #ifdef GL_EXT_subtexture
- /* pass NULL image (this is an extension) */
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
- sWidth, sHeight, 0, 0, 0, NULL); /* format, type irrelevant */
-
- glTexSubImage2DEXT( GL_TEXTURE_2D, 0,
- subx, suby, subwidth, subheight,
- GL_RGBA, GL_UNSIGNED_BYTE, subImage);
- #endif
- glBegin(GL_QUADS);
- glTexCoord2f( 0, 0 ); glVertex2f( 0, 0 );
- glTexCoord2f( 1, 0 ); glVertex2f( winWidth, 0 );
- glTexCoord2f( 1, 1 ); glVertex2f( winWidth, winHeight );
- glTexCoord2f( 0, 1 ); glVertex2f( 0, winHeight );
- glEnd();
- glFlush();
- }
-